Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 429)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.98766 12.98518 12.98271 12.98026 12.97783 12.97542 12.97303 12.97067
##   [9] 12.96832 12.96600 12.96370 12.96143 12.95918 12.95697 12.95477 12.95261
##  [17] 12.95048 12.94838 12.94630 12.94427 12.94226 12.94029 12.93835 12.93645
##  [25] 12.93458 12.93276 12.93097 12.92922 12.92751 12.92584 12.92422 12.92263
##  [33] 12.92110 12.91960 12.91815 12.91675 12.91539 12.91409 12.91283 12.91162
##  [41] 12.91046 12.90936 12.90831 12.90731 12.90639 12.90553 12.90474 12.90400
##  [49] 12.90333 12.90271 12.90216 12.90165 12.90120 12.90080 12.90045 12.90015
##  [57] 12.89989 12.89968 12.89951 12.89938 12.89929 12.89924 12.89922 12.89923
##  [65] 12.89928 12.89935 12.89945 12.89958 12.89974 12.89991 12.90011 12.90032
##  [73] 12.90055 12.90080 12.90106 12.90133 12.90162 12.90191 12.90220 12.90251
##  [81] 12.90281 12.90311 12.90342 12.90372 12.90402 12.90437 12.90484 12.90541
##  [89] 12.90610 12.90688 12.90776 12.90873 12.90979 12.91094 12.91217 12.91347
##  [97] 12.91484 12.91628 12.91778 12.91934 12.92096 12.92262 12.92433 12.92608
## [105] 12.92787 12.92969 12.93154 12.93342 12.93531 12.93722 12.93914 12.94106
## [113] 12.94299 12.94492 12.94684 12.94875 12.95065 12.95253 12.95438 12.95621
## [121] 12.95801 12.95977 12.96149 12.96316 12.96479 12.96636 12.96788 12.96933
## [129] 12.97072 12.97204 12.97328 12.97445 12.97553 12.97716 12.97989 12.98361
## [137] 12.98819 12.99353 12.99950 13.00598 13.01285 13.02000 13.02732 13.03467
## [145] 13.04195 13.04903 13.05580 13.06213 13.06792 13.07304 13.07738 13.08081
## [153] 13.08322 13.08449 13.08577 13.08824 13.09178 13.09631 13.10174 13.10795
## [161] 13.11486 13.12236 13.13037 13.13878 13.14749 13.15642 13.16546 13.17451
## [169] 13.18348 13.19227 13.20079 13.20894 13.21661 13.22372 13.23017 13.23585
## [177] 13.24068 13.24455 13.24737 13.24904 13.24947 13.24855 13.24663 13.24413
## [185] 13.24107 13.23749 13.23340 13.22883 13.22381 13.21836 13.21251 13.20629
## [193] 13.19972 13.19282 13.18562 13.17815 13.17044 13.16250 13.15436 13.14606
## [201] 13.13761 13.12904 13.12037 13.11164 13.10287 13.09407 13.08529 13.07654
## [209] 13.06786 13.05926 13.05015 13.03998 13.02878 13.01660 13.00349 12.98949
## [217] 12.97464 12.95901 12.94262 12.92553 12.90778 12.88942 12.87050 12.85105
## [225] 12.83114 12.81079 12.79007 12.76901 12.74766 12.72607 12.70428 12.68234
## [233] 12.66030 12.63820 12.61608 12.59400 12.57200 12.55012 12.52842 12.50693
## [241] 12.48571 12.46480 12.44424 12.42409 12.40438 12.38517 12.36650 12.34841
## [249] 12.33096 12.31419 12.29814 12.28286 12.26790 12.25281 12.23761 12.22232
## [257] 12.20695 12.19153 12.17609 12.16064 12.14520 12.12979 12.11445 12.09918
## [265] 12.08401 12.06896 12.05405 12.03930 12.02474 12.01039 11.99626 11.98238
## [273] 11.96877 11.95545 11.94244 11.92992 11.91804 11.90673 11.89596 11.88568
## [281] 11.87584 11.86641 11.85732 11.84854 11.84001 11.83170 11.82356 11.81554
## [289] 11.80759 11.79968 11.79174 11.78374 11.77564 11.76737 11.75891 11.75020
## [297] 11.74118 11.73185 11.72225 11.71239 11.70232 11.69206 11.68165 11.67111
## [305] 11.66047 11.64977 11.63903 11.62830 11.61759 11.60694 11.59637 11.58593
## [313] 11.57564 11.56552 11.55562 11.54596 11.53658 11.52750 11.51875 11.51036
## [321] 11.50238 11.49482 11.48771 11.48109 11.47499 11.46944 11.46447 11.46011
## [329] 11.45639 11.45335 11.45100 11.44939 11.44854 11.44848 11.44925 11.45088
## [337] 11.45339 11.45682 11.46081 11.46501 11.46942 11.47407 11.47897 11.48414
## [345] 11.48960 11.49536 11.50144 11.50786 11.51463 11.52177 11.52930 11.53724
## [353] 11.54560 11.55439 11.56364 11.57336 11.58358 11.59429 11.60553 11.61731
## [361] 11.62965 11.64256 11.65606 11.67017 11.68471 11.69950 11.71458 11.72994
## [369] 11.74560 11.76159 11.77792 11.79460 11.81166 11.82910 11.84695 11.86522
## [377] 11.88392 11.90308 11.92271 11.94282 11.96344 11.98458 12.00625 12.02847
## [385] 12.05126 12.07464 12.09861 12.12313 12.14811 12.17356 12.19947 12.22585
## [393] 12.25270 12.28001 12.30778 12.33602 12.36472 12.39388 12.42351 12.45360
## [401] 12.48415 12.51516 12.54663 12.57856 12.61095 12.64379 12.67710 12.71086
## [409] 12.74509 12.77976 12.81490 12.85049 12.88654 12.92304 12.95999 12.99740
## [417] 13.03526 13.07358 13.11235 13.15157 13.19124 13.23136 13.27194 13.31296
## [425] 13.35443 13.39636 13.43873 13.48155 13.52481
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 429)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.48421 12.48497 12.48573 12.48650 12.48727 12.48805 12.48885 12.48965
##   [9] 12.49047 12.49130 12.49215 12.49302 12.49390 12.49481 12.49575 12.49670
##  [17] 12.49769 12.49870 12.49975 12.50082 12.50193 12.50307 12.50425 12.50547
##  [25] 12.50673 12.50803 12.50937 12.51076 12.51219 12.51367 12.51520 12.51679
##  [33] 12.51842 12.52012 12.52186 12.52367 12.52554 12.52746 12.52946 12.53151
##  [41] 12.53363 12.53582 12.53808 12.54041 12.54282 12.54529 12.54783 12.55043
##  [49] 12.55309 12.55582 12.55860 12.56143 12.56432 12.56726 12.57025 12.57328
##  [57] 12.57635 12.57947 12.58262 12.58581 12.58904 12.59229 12.59558 12.59889
##  [65] 12.60222 12.60558 12.60896 12.61235 12.61576 12.61918 12.62261 12.62605
##  [73] 12.62950 12.63295 12.63639 12.63984 12.64329 12.64672 12.65015 12.65357
##  [81] 12.65698 12.66037 12.66374 12.66709 12.67042 12.67385 12.67750 12.68136
##  [89] 12.68542 12.68967 12.69410 12.69869 12.70344 12.70834 12.71337 12.71853
##  [97] 12.72381 12.72919 12.73466 12.74021 12.74584 12.75153 12.75727 12.76305
## [105] 12.76886 12.77469 12.78053 12.78637 12.79220 12.79800 12.80377 12.80950
## [113] 12.81517 12.82077 12.82631 12.83175 12.83710 12.84234 12.84746 12.85246
## [121] 12.85731 12.86202 12.86656 12.87094 12.87513 12.87913 12.88293 12.88651
## [129] 12.89045 12.89523 12.90078 12.90699 12.91376 12.92100 12.92862 12.93652
## [137] 12.94460 12.95278 12.96095 12.96901 12.97688 12.98445 12.99164 12.99834
## [145] 13.00447 13.00992 13.01460 13.01841 13.02126 13.02399 13.02745 13.03157
## [153] 13.03628 13.04152 13.04720 13.05327 13.05964 13.06625 13.07304 13.07992
## [161] 13.08683 13.09371 13.10047 13.10705 13.11338 13.11938 13.12500 13.13015
## [169] 13.13477 13.13879 13.14213 13.14474 13.14653 13.14743 13.14739 13.14672
## [177] 13.14579 13.14463 13.14321 13.14156 13.13967 13.13754 13.13517 13.13257
## [185] 13.12974 13.12668 13.12340 13.11990 13.11617 13.11222 13.10806 13.10369
## [193] 13.09910 13.09430 13.08930 13.08409 13.07868 13.07307 13.06727 13.06127
## [201] 13.05507 13.04869 13.04212 13.03536 13.02842 13.02081 13.01209 13.00231
## [209] 12.99152 12.97976 12.96709 12.95356 12.93921 12.92411 12.90828 12.89180
## [217] 12.87470 12.85703 12.83886 12.82021 12.80116 12.78174 12.76200 12.74200
## [225] 12.72179 12.70141 12.68091 12.66035 12.63978 12.61924 12.59878 12.57846
## [233] 12.55833 12.53842 12.51880 12.49952 12.48062 12.46215 12.44417 12.42671
## [241] 12.40985 12.39361 12.37806 12.36324 12.34921 12.33600 12.32368 12.31194
## [249] 12.30044 12.28915 12.27807 12.26719 12.25649 12.24596 12.23558 12.22534
## [257] 12.21524 12.20524 12.19535 12.18555 12.17583 12.16616 12.15655 12.14697
## [265] 12.13741 12.12786 12.11831 12.10874 12.09914 12.08949 12.07979 12.07002
## [273] 12.06016 12.05062 12.04177 12.03355 12.02589 12.01875 12.01205 12.00574
## [281] 11.99977 11.99406 11.98856 11.98322 11.97796 11.97274 11.96749 11.96214
## [289] 11.95665 11.95096 11.94499 11.93870 11.93202 11.92489 11.91740 11.90968
## [297] 11.90178 11.89372 11.88555 11.87730 11.86900 11.86069 11.85240 11.84418
## [305] 11.83605 11.82805 11.82022 11.81259 11.80520 11.79808 11.79126 11.78480
## [313] 11.77871 11.77303 11.76781 11.76307 11.75885 11.75426 11.74850 11.74171
## [321] 11.73406 11.72570 11.71678 11.70748 11.69794 11.68831 11.67877 11.66946
## [329] 11.66054 11.65216 11.64450 11.63769 11.63190 11.62729 11.62401 11.62222
## [337] 11.62208 11.62374 11.62639 11.62911 11.63194 11.63490 11.63800 11.64127
## [345] 11.64473 11.64840 11.65231 11.65648 11.66093 11.66568 11.67076 11.67619
## [353] 11.68199 11.68817 11.69478 11.70182 11.70932 11.71731 11.72579 11.73481
## [361] 11.74437 11.75451 11.76524 11.77658 11.78832 11.80024 11.81234 11.82465
## [369] 11.83719 11.84998 11.86304 11.87639 11.89005 11.90404 11.91837 11.93307
## [377] 11.94816 11.96366 11.97958 11.99594 12.01278 12.03010 12.04792 12.06627
## [385] 12.08516 12.10461 12.12465 12.14520 12.16618 12.18758 12.20940 12.23165
## [393] 12.25432 12.27741 12.30092 12.32485 12.34921 12.37398 12.39917 12.42478
## [401] 12.45081 12.47725 12.50411 12.53139 12.55908 12.58718 12.61570 12.64463
## [409] 12.67398 12.70373 12.73390 12.76448 12.79547 12.82686 12.85867 12.89088
## [417] 12.92350 12.95653 12.98997 13.02381 13.05805 13.09270 13.12775 13.16321
## [425] 13.19906 13.23532 13.27198 13.30904 13.34650
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 429)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.95019 11.94692 11.94370 11.94054 11.93742 11.93436 11.93134 11.92839
##   [9] 11.92549 11.92264 11.91985 11.91712 11.91445 11.91183 11.90928 11.90679
##  [17] 11.90436 11.90200 11.89969 11.89746 11.89529 11.89319 11.89115 11.88919
##  [25] 11.88729 11.88547 11.88371 11.88203 11.88043 11.87890 11.87744 11.87606
##  [33] 11.87476 11.87353 11.87239 11.87133 11.87034 11.86944 11.86862 11.86789
##  [41] 11.86724 11.86668 11.86620 11.86581 11.86551 11.86530 11.86518 11.86515
##  [49] 11.86522 11.86537 11.86565 11.86607 11.86663 11.86733 11.86816 11.86912
##  [57] 11.87020 11.87139 11.87271 11.87413 11.87566 11.87729 11.87902 11.88084
##  [65] 11.88275 11.88475 11.88682 11.88897 11.89120 11.89349 11.89585 11.89826
##  [73] 11.90073 11.90326 11.90582 11.90844 11.91108 11.91377 11.91648 11.91922
##  [81] 11.92198 11.92476 11.92755 11.93034 11.93315 11.93595 11.93875 11.94154
##  [89] 11.94431 11.94707 11.94981 11.95253 11.95530 11.95821 11.96125 11.96443
##  [97] 11.96773 11.97116 11.97469 11.97834 11.98210 11.98596 11.98992 11.99397
## [105] 11.99811 12.00233 12.00664 12.01101 12.01546 12.01997 12.02454 12.02917
## [113] 12.03385 12.03858 12.04334 12.04815 12.05298 12.05785 12.06274 12.06764
## [121] 12.07257 12.07750 12.08243 12.08737 12.09230 12.09722 12.10212 12.10701
## [129] 12.11188 12.11671 12.12152 12.12628 12.13101 12.13662 12.14395 12.15281
## [137] 12.16303 12.17445 12.18689 12.20017 12.21413 12.22859 12.24338 12.25833
## [145] 12.27326 12.28801 12.30240 12.31625 12.32940 12.34167 12.35289 12.36289
## [153] 12.37149 12.37853 12.38567 12.39460 12.40517 12.41725 12.43068 12.44532
## [161] 12.46101 12.47762 12.49500 12.51299 12.53146 12.55026 12.56924 12.58826
## [169] 12.60716 12.62581 12.64405 12.66175 12.67874 12.69489 12.71006 12.72408
## [177] 12.73683 12.74814 12.75788 12.76590 12.77205 12.77619 12.77865 12.77992
## [185] 12.78006 12.77913 12.77720 12.77434 12.77060 12.76605 12.76076 12.75479
## [193] 12.74821 12.74108 12.73346 12.72542 12.71702 12.70833 12.69941 12.69033
## [201] 12.68115 12.67193 12.66274 12.65365 12.64472 12.63601 12.62758 12.61951
## [209] 12.61185 12.60468 12.59714 12.58841 12.57853 12.56755 12.55553 12.54251
## [217] 12.52854 12.51369 12.49799 12.48149 12.46426 12.44634 12.42779 12.40864
## [225] 12.38896 12.36880 12.34821 12.32723 12.30592 12.28433 12.26252 12.24053
## [233] 12.21841 12.19622 12.17400 12.15181 12.12970 12.10772 12.08592 12.06435
## [241] 12.04307 12.02212 12.00155 11.98143 11.96179 11.94269 11.92418 11.90631
## [249] 11.88913 11.87270 11.85706 11.84227 11.82772 11.81282 11.79759 11.78209
## [257] 11.76636 11.75045 11.73440 11.71826 11.70206 11.68585 11.66969 11.65360
## [265] 11.63765 11.62186 11.60629 11.59098 11.57598 11.56132 11.54705 11.53323
## [273] 11.51988 11.50706 11.49481 11.48303 11.47156 11.46040 11.44953 11.43895
## [281] 11.42865 11.41862 11.40884 11.39931 11.39002 11.38095 11.37210 11.36346
## [289] 11.35501 11.34675 11.33867 11.33076 11.32300 11.31538 11.30791 11.30056
## [297] 11.29324 11.28587 11.27846 11.27105 11.26363 11.25623 11.24886 11.24154
## [305] 11.23429 11.22712 11.22005 11.21310 11.20628 11.19961 11.19311 11.18679
## [313] 11.18067 11.17477 11.16909 11.16367 11.15851 11.15364 11.14906 11.14480
## [321] 11.14087 11.13729 11.13408 11.13124 11.12881 11.12679 11.12520 11.12406
## [329] 11.12338 11.12319 11.12349 11.12431 11.12566 11.12756 11.13002 11.13306
## [337] 11.13670 11.14095 11.14559 11.15040 11.15538 11.16056 11.16595 11.17155
## [345] 11.17740 11.18350 11.18987 11.19652 11.20346 11.21072 11.21830 11.22623
## [353] 11.23451 11.24316 11.25220 11.26163 11.27149 11.28177 11.29249 11.30368
## [361] 11.31534 11.32749 11.34014 11.35331 11.36685 11.38064 11.39467 11.40897
## [369] 11.42353 11.43838 11.45353 11.46898 11.48475 11.50086 11.51730 11.53411
## [377] 11.55127 11.56882 11.58676 11.60510 11.62386 11.64304 11.66266 11.68273
## [385] 11.70326 11.72426 11.74575 11.76768 11.78999 11.81268 11.83576 11.85922
## [393] 11.88306 11.90729 11.93189 11.95688 11.98224 12.00799 12.03411 12.06062
## [401] 12.08750 12.11476 12.14240 12.17041 12.19881 12.22757 12.25672 12.28624
## [409] 12.31614 12.34641 12.37706 12.40808 12.43947 12.47124 12.50338 12.53589
## [417] 12.56877 12.60203 12.63566 12.66966 12.70402 12.73876 12.77387 12.80935
## [425] 12.84519 12.88141 12.91799 12.95494 12.99226
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")